package cn.stylefeng.guns.modular.system.service.impl;

import cn.stylefeng.guns.base.pojo.node.ZTreeNode;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.system.entity.ShiWarehouse;
import cn.stylefeng.guns.modular.system.mapper.ShiWarehouseMapper;
import cn.stylefeng.guns.modular.system.model.params.ShiWarehouseParam;
import cn.stylefeng.guns.modular.system.model.result.ShiWarehouseResult;
import cn.stylefeng.guns.modular.system.service.ShiWarehouseService;
import cn.stylefeng.guns.sys.modular.system.mapper.DeptMapper;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 仓库表 服务实现类
 * </p>
 *
 * @author 小飞
 * @since 2019-06-22
 */
@Service
public class ShiWarehouseServiceImpl extends ServiceImpl<ShiWarehouseMapper, ShiWarehouse> implements ShiWarehouseService {

    @Autowired
    private DeptMapper deptMapper;

    @Override
    public void add(ShiWarehouseParam param, Long deptId) {
        ShiWarehouse entity = getEntity(param);
        entity.setIsDelete(0);
        entity.setPid(deptId);

        //自动生成编号
        ShiWarehouse shiWarehouseLast = this.getOne(new QueryWrapper<ShiWarehouse>()
                .orderByDesc("id").last("limit 1"));
        if (ToolUtil.isNotEmpty(shiWarehouseLast)) {
            Long num = shiWarehouseLast.getId() + 1;
            entity.setSerialNum("CK00".concat(num.toString()));
        } else {
            entity.setSerialNum("CK000");
        }
        this.save(entity);
    }

    @Override
    public void delete(ShiWarehouseParam param) {
        ShiWarehouse shiWarehouse = this.getById(getKey(param));
        if (ToolUtil.isNotEmpty(shiWarehouse)) {
            shiWarehouse.setIsDelete(1);
            this.updateById(shiWarehouse);
        }
    }

    @Override
    public void update(ShiWarehouseParam param) {
        ShiWarehouse oldEntity = getOldEntity(param);
        ShiWarehouse newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public ShiWarehouseResult findBySpec(ShiWarehouseParam param) {
        return null;
    }

    @Override
    public List<ShiWarehouseResult> findListBySpec(ShiWarehouseParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(ShiWarehouseParam param, List<Long> idList) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param, idList);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public List<ZTreeNode> tree() {
        return baseMapper.tree();
    }

    private Serializable getKey(ShiWarehouseParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private ShiWarehouse getOldEntity(ShiWarehouseParam param) {
        return this.getById(getKey(param));
    }

    private ShiWarehouse getEntity(ShiWarehouseParam param) {
        ShiWarehouse entity = new ShiWarehouse();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
